/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package YottaTron.Sprites;

import YottaTron.Equipment.Engine.Engine;
import YottaTron.Equipment.Gear.Gear;
import YottaTron.Equipment.Torso.Torso;
import YottaTron.Equipment.Weapons.Weapon;
import YottaTron.ImagesPlayer;
import YottaTron.Runner;
import YottaTron.Strategies.Strategy;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Cenda
 */
public class BotSprite extends Sprite{
    
    Runner runner;
    
    public ArrayList<Weapon> weapons;
    public Engine engine;
    public Gear gear;
    public Torso torso;
    private boolean kolize = false;
    boolean shooting;
    public int aimX,aimY;
    double aimAngle;
    public boolean burning;
    public Strategy strategy;
   

    public BotSprite(double x, double y, int width, int height, int ID, Runner runner) {
        super(x, y, width, height, ID);
        this.runner = runner;
        weapons = new ArrayList<Weapon>();
        collisionDamage = 2;
        setDirection(0);
        setSpeed(0);
        burning = false;
        }

     public void addWeapon(Weapon weapon){
    weapons.add(weapon);
    }

    public void addGear(Gear gear){
    this.gear = gear;
    }

    public void addEngine(Engine engine){
    this.engine = engine;
    }

    public void addTorso(Torso torso){
    this.torso = torso;
       collisionDamage = torso.collisionDamage;
    }

    @Override
    public void handleCollision(Sprite target){
        removeAllActions();
        kolize = true;
        //pokud hraci oba narazi do sebe
        if(target!=null){
        target.damage(Math.abs((int) (target.getSpeed() * collisionDamage)) + Math.abs((int) (getSpeed() * target.collisionDamage)));
            this.damage(Math.abs((int) (getSpeed() * collisionDamage)) + Math.abs((int) (target.getSpeed() * collisionDamage)));
        }


        checkHitPoints();
        setSpeed(0);
    }

    public int getHitPoints() {
        return torso.hitPoints;
    }

    public void speedUP() {
        if(!kolize){
        if (getSpeed() < engine.maxSpeed) {
            if (getSpeed() < 0) {
                setSpeed(getSpeed() + gear.brakePower);
            }else{
            setSpeed(getSpeed() + engine.acceleration);
            }

        }
        }else{
            kolize = false;

        }
    }

    public void speedDown() {
         if(!kolize){
        if (getSpeed() > -engine.maxSpeed) {
            if (getSpeed() > 0) {
                setSpeed(getSpeed() - gear.brakePower);
            }else{
            setSpeed(getSpeed() - engine.acceleration);
            }

        }
        }else{
        kolize = false;
        }
    }

    public void turnLeft() {
        double newDirection = getDirection() - gear.turningSpeed;
        changeDirection(newDirection);
        torso.graphics.rotateShape(newDirection);

    }

    public void turnRight() {
       double newDirection = getDirection() + gear.turningSpeed;
        changeDirection(newDirection);
        torso.graphics.rotateShape(newDirection);
    }

    public void checkHitPoints() {
        if (torso.hitPoints < torso.maxHitPoints*0.2&&!burning) {
            burning=true;
        }
        if (torso.hitPoints < 0) {
            destroySprite(runner);
        }
    }

    public void setHitPoints(int hitPoints) {
        torso.hitPoints = hitPoints;
    }


    @Override
    public void damage(int damage){
    torso.hitPoints = torso.hitPoints - damage;
    checkHitPoints();
    }


    public int getAimX() {
        return aimX;
    }

    public void setAimX(int aimX) {
        this.aimX = aimX;
    }

    public int getAimY() {
        return aimY;
    }

    public void setAimY(int aimY) {
        this.aimY = aimY;
    }

    public void setAimAngle(){
        aimAngle = -Math.atan2(getCenterX() - aimX, getCenterY() - aimY);

         if (aimAngle < 0) {
            aimAngle = aimAngle + Math.PI * 2;
        }
        if (aimAngle > Math.PI * 2) {
            aimAngle = aimAngle - Math.PI * 2;
        }

    }
    public double getAimAngle(){
    return aimAngle;
    }

    public ArrayList<Weapon> getWeapons() {
        return weapons;
    }

    @Override
    public void destroySprite(Runner runner){

        active = false;
        runner.removeBot(this);
        runner.dePopulate(this);
        runner.ShowAnimationAt(new ImagesPlayer(runner.animationGraphics[1],1d,1,runner.globalTickTime,false,runner), (int)getX(), (int)getY());
    }

    public boolean isShooting() {
        return shooting;
    }

    public void setShooting(boolean isShooting) {
        this.shooting = isShooting;
    }

    public double getMaxSpeed(){
    return engine.maxSpeed;
    }

    public double getAcceleration(){
    return engine.acceleration;
    }

    @Override
    public ArrayList<Point2D> getShape(){
    return torso.graphics.getShape();
    }

    //zjisti na kterych polickach herniPole se sprite nachazi
    @Override
    public ArrayList<Point2D> getOccupiedCells(){
    ArrayList<Point2D> occupiedCells = new ArrayList<Point2D>();
        for (Iterator<Point2D> it = torso.graphics.getShape().iterator(); it.hasNext();) {
            Point2D point = it.next();
            occupiedCells.add(new Point(runner.XToTiles((int) (point.getX() + getX())),runner.YToTiles((int) (point.getY() + getY()))));
        }
    return occupiedCells;
    }

}
